import { validatenull } from "@/libs/validate";
import website from "@/const/website";

const keyName = website.key + "-";
/**
 * 存储localStorage
 */
export const setStore = (params = {}) => {
  let { name, content } = params;
  name = keyName + name;
  let obj = {
    dataType: typeof content,
    content: content,
    datetime: new Date().getTime(),
  };
  uni.setStorageSync(name, JSON.stringify(obj));
};
/**
 * 获取localStorage
 */

export const getStore = (params = {}) => {
  let { name, debug } = params;
  name = keyName + name;
  let obj = {},
    content;
  obj = uni.getStorageSync(name);
  if (validatenull(obj)) return;
  try {
    obj = JSON.parse(obj);
  } catch {
    return obj;
  }
  if (debug) {
    return obj;
  }
  if (obj.dataType == "string") {
    content = obj.content;
  } else if (obj.dataType == "number") {
    content = Number(obj.content);
  } else if (obj.dataType == "boolean") {
    content = eval(obj.content);
  } else if (obj.dataType == "object") {
    content = obj.content;
  }
  return content;
};
/**
 * 删除localStorage
 */
export const removeStore = (params = {}) => {
  let { name } = params;
  name = keyName + name;
  uni.removeStorageSync(name);
};
